package org.proxywars.engine.test;

import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.controls.GameControl;
import com.jme.input.controls.GameControlManager;
import com.jme.input.controls.binding.KeyboardBinding;
import com.jme.input.controls.controller.ActionChangeController;
import com.jme.input.controls.controller.ControlChangeListener;
import com.jmex.game.state.BasicGameState;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;

/**
 * Our InputGameState.
 * For comparison we use two different Input systems.
 * 
 * To Enable/disable the TextGamestate (F1) we use the GamecontrolSystem.
 * For the other Keys, the easier to use KeyBindingManager is used.
 * 
 * Another of Core-Dump's demo files I borrowed for testing.
 */
public class InputGameState extends BasicGameState {
    /**
     * constructor, creates the Inputhandler and actions to listen for.
     */
    public InputGameState(String name) {
        super(name);
        
        // the GameControl System:
        // 1. create GameControlManager, needed to create controls from 
        GameControlManager manager = new GameControlManager();
        // 2. create a GameControl as input for the Controller later
        GameControl textTogglecontrol = manager.addControl("toggle_text");
        // 3. add a Key Binding to the gameControl (F1)
        textTogglecontrol.addBinding(new KeyboardBinding(KeyInput.KEY_F1));
        // 4. create a Controller, which gets the InputValues from the GameControl
        ActionChangeController textToggleAction = new ActionChangeController(textTogglecontrol, new ControlChangeListener() {
            public void changed(GameControl control, float oldValue,
                    float newValue, float time) {
                // toggle TextGameState active/inactive
                // get the GameState
                GameState textGS = GameStateManager.getInstance().getChild("text");
                // invert the GameStates active flag
                textGS.setActive(!textGS.isActive());
            }
        });
        // 5. add the controller to a node, so that it gets updated every update cycle.
        rootNode.addController(textToggleAction);
        
        // adding new actions to the KeybindingManager
        KeyBindingManager.getKeyBindingManager().add("toggle_3d", KeyInput.KEY_F2);
        KeyBindingManager.getKeyBindingManager().add("toggle_stat", KeyInput.KEY_F3);
        KeyBindingManager.getKeyBindingManager().set("exit", KeyInput.KEY_ESCAPE);
    }
    
    /**
     * Check if the a Key that we defined previously, has been pressed and execute the according action
     */
    public void update(float tpf) {
        super.update(tpf);
        // check if F2 has been pressed and toggle the 3D Gamestate
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("toggle_3d", false)) {
            // invert the active flag of the 3D GameState
            GameStateManager.getInstance().getChild("3d").setActive(
                    !GameStateManager.getInstance().getChild("3d").isActive());
        }
        
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("toggle_stat", false)) {
            // invert the active flag of the 3D GameState
            GameStateManager.getInstance().getChild("statistics").setActive(
                    !GameStateManager.getInstance().getChild("statistics").isActive());
        }
        
        // check if ESC has been pressed, exit the application if needed 
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit", false)) {
            System.out.println("Good Bye");
            System.exit(0);
        }
    }
}
